<?php

class Forum extends CI_Controller{
function index(){
$this->cek_login();
$query="select * from frm_category";
$result=$this->db->query($query);
$categories=array();
$catnum=1;
foreach ($result->result() as $category){
$id=$category->id;
$nama=$category->nama;
$deskripsi=$category->deskripsi;
$categories[$catnum]=array($id,$nama,$deskripsi);
$catnum++;
}
$data['categories']=$categories;
$this->load->view('forum',$data);


}
function cek_login(){
if($this->session->userdata('login')!=TRUE){
$this->load->view('login');
}
}

function category($id=0){
$data['cat_id']=$id;
$query="select * from frm_thread where `category`='$id'";
$result=$this->db->query($query);
$threads=array();
$t_id=1;
foreach ($result->result() as $thread){

$id=$thread->id;
$subjek=$thread->subject;
$tanggal=$thread->tanggal;
$oleh=$thread->oleh;
$category=$thread->category;
$query="select `isi` from frm_reply where `thread`='$id' limit 1";
$res=$this->db->query($query);
foreach ($res->result() as $a){
$isi=$a->isi;
}
$threads[$t_id]=array($id,$subjek,$tanggal,$oleh,$category,$isi);
$t_id++;
}
$data['threads']=$threads;
$this->load->view('frm-category',$data);

}


function new_category(){


}
function create_thread(){
$cat_id= $this->input->post('cat_id');
$user_id= $this->session->userdata('userid');
$subjek=$this->input->post('subjek',TRUE);
$content=$this->input->post('content',TRUE);
$t= date("YmdHis");
$query="insert into frm_thread (`subject`,`tanggal`,`oleh`,`category`) values ('$subjek','$t','$user_id','$cat_id')";
$this->db->query($query);
$thread_id= $this->db->insert_id();
$query="insert into frm_reply (`isi`,`tanggal`,`thread`,`oleh`) values ('$content','$t','$thread_id','$user_id')";
$this->db->query($query);
$u='forum/thread/'.$thread_id;
redirect($u);


}
function new_thread($cat_id =0){
if ($cat_id==0){
$this->load->view('forum');
}else{
$data['cat_id']=$cat_id;
$this->load->view('new_thread',$data);

}

}
function thread($thread_id){
$query="select * from frm_thread where `id` = '$thread_id'";
$result=$this->db->query($query);
if ($result->num_rows()==1){
foreach($result->result() as $thrd){
 $data['thd_subjek']=$thrd->subject;
$data['tanggal']=$thrd->tanggal;
$data['oleh']=$thrd->oleh;
$data['cat']=$thrd->category;
}
}
$query="select * from frm_reply where `thread`='$thread_id'";
$result=$this->db->query($query);
$replies=array();
$rep_num=1;
foreach($result->result() as $reply){
	$id=$reply->id;
	$isi=$reply->isi;
	$tanggal=$reply->tanggal;
	$thread=$reply->thread;
	$oleh=$reply->oleh;
	$replies[$rep_num]=array($id,$isi,$tanggal,$thread,$oleh);
$rep_num++;
}
$data['replies']=$replies;
$this->load->view('thread',$data);
}

}
